package edu.bit.water.service;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.bit.water.entity.*;
import edu.bit.water.util.ColumnIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import edu.bit.water.dao.MetaSensorDao;
import edu.bit.water.util.EntityCloumnList;

@Service
@Component
public class MetaSensorService {
	@Autowired
	public MetaSensorDao metaSensorDao;
	@Autowired
	public MetaSensorAssociationService metaSensorAssociationService;
	@Autowired
	public MetaSensorService metaSensorService;
	//private boolean flag = false; // 用来记录实体表字段是否发生改变，true为已改变，false为改变
	//private List<Column> list = null;

	/**
	 * @return 返回所有字段包括类型
	 */
	public List<Column> selectColumn(String techProcessId) {
		List<Column> list= metaSensorDao.selectColumn(techProcessId);
		return list;
	}

	/**
	 *
	 * @param techProcessId
	 * @return
	 */
	public List<MetaSensor> selectData(String techProcessId) {
		return metaSensorDao.selectData(techProcessId);
	}
	/**
	 * @return 返回所有未删除字段包括类型
	 */
	public List<Column> selectRealColumn(int company_id) {
//		System.out.println("selectColumn未删除");
		List<Column> list= metaSensorDao.selectRealColumn(company_id);

//		System.out.println(EntityCloumnList.isFlag());
//		if (EntityCloumnList.getList() == null || EntityCloumnList.isFlag() ) {
//			System.out.println("selectColumn哈");
//			List<Column> list= metaSensorDao.selectColumn(company_id);
//			for(int j=0;j<list.size();j++){
//				System.out.println("service层");
//				System.out.println(list.get(j).csName);
//				System.out.println(list.get(j).enName);
//			}
//			EntityCloumnList.setList(list);
//			EntityCloumnList.setFlag(false);
//		}
		return list;
	}

	/**
	 * @return 返回list
	 */
	public ArrayList<MetaSensor> queryMetaSensor(int company_id) {
		ArrayList<MetaSensor> list = metaSensorDao.queryMetaSensor(company_id);
		return list;
	}

	/**
	 * @param metaSensor 元数据设备实体类
	 * @return 成功返回1，失败返回0
	 * @throws SQLIntegrityConstraintViolationException
	 */
	public Map<String, Object> insertMetaSensor(MetaSensor metaSensor)  {
		String columnId = ColumnIdUtil.getRandomString(16);
		while(metaSensorDao.queryColumnId(columnId) > 1) {
			columnId = ColumnIdUtil.getRandomString(16);
		}
		metaSensor.setColumnDefination(columnId);
		Map<String, Object> modelMap = new HashMap<>();
		try {
			metaSensorDao.insertMetaSensor(metaSensor);
		} catch (DuplicateKeyException e) {
			modelMap.put("status", "failed");
			if(e.getMessage().indexOf("UNIQUE_CS") != -1) {
				modelMap.put("msg", "中文名称已存在，请重新输入");
				return modelMap;
			}
			else if (e.getMessage().indexOf("UNIQUE_EN") != -1) {
				modelMap.put("msg", "英文名称已存在，请重新输入");
				return modelMap;
			}
		}
		if(metaSensor.getDataType().equals("字符型"))
			metaSensor.setDataType("varchar(100)");
		else if (metaSensor.getDataType().equals("小数型")){
			metaSensor.setDataType("float");
		}
		else if (metaSensor.getDataType().equals("整数型")){
			metaSensor.setDataType("integer");
		}
		metaSensorAssociationService.insertMetaSensorAssociation(metaSensor);

		modelMap.put("status", "success");
		EntityCloumnList.setFlag(true);
		return modelMap;
	}
//	public Map<String, Object> insertMetaSensorService(MetaSensor metaSensor) {
//		//metaSensor.dateChange();
//		Map<String, Object> result = new HashMap<>();
//		Integer num = metaSensorDao.getMaxId();
//		System.out.println(num);
//		if (num != null) {
//			metaSensor.setId(num + 1);
//		}
//		else {
//			metaSensor.setId(0);
//		}
//		int tmp = metaSensorDao.insertMetaSensor(metaSensor);
//		if (tmp == 1) {
//			result.put("status", "success");
//		}else {
//			result.put("status", "failed");
//		}
//		return result;
//	}
	/**
	 * @return 成功返回1，失败返回0
	 */
	public int deleteMetaSensor(List<String> idList) {
		try {
			for (String id : idList) {
				MetaSensor entity = metaSensorDao.getById(Integer.parseInt(id));
				if("0".equals(entity.getTechProcessId())) {
					throw new Exception("不能删除基本列元素");
				}
				metaSensorAssociationService.deleteMetaSensorAssociation(metaSensorDao.getById(Integer.parseInt(id)));
				metaSensorDao.deleteMetaSensor(id);
			}
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}finally {
			EntityCloumnList.setFlag(true);
		}
	}

	//	/**
//	 * @param enName 字段英文名称
//	 * @return 成功返回1，失败返回0
//	 */
//	public int deleteMetaSensor(String enName) {
//		int n = metaSensorDao.deleteMetaSensor(enName);
//		EntityCloumnList.setFlag(true);
//		return n;
//	}
	public Map<String, Object> updateMetaSensor(MetaSensor MetaSensor) {
		Map<String, Object> result = new HashMap<>();
//		System.out.println(MetaSensor.getId());
		MetaSensor entiy = metaSensorDao.getById(MetaSensor.getId());
//		System.out.println(entiy.getEnName());
		if("0".equals(entiy.getTechProcessId())) {
			result.put("status", "failed");
			result.put("msg", "不能修改基本列元素");
			return result;
		}
		int tmp = metaSensorDao.updateMetaSensor(MetaSensor);
		if(tmp == 1) {
			result.put("status", "success");
		}
		else {
			result.put("status", "failed");
		}
		return result;
	}

	public String getCodeByCSName(String csName){
		MetaSensor entity = metaSensorDao.getCodeByCSName(csName);
		String code = entity.getColumnDefination();
		return code;
	}

}
